//
//  CountdownAnimationView.swift
//  DaFit
//
//  Created by 魔样科技 on 2022/9/15.
//  Copyright © 2022 sylar. All rights reserved.
//

import Foundation
import UIKit
import AVFAudio
import MYModule

open class CountdownAnimationView: View {
    
    private var done: (() -> ())?
    
    
    private var mtitleArr: [String]!
    
    open override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = .white
        self.mtitleArr = ["3", "2", "1", "GO"]
        self.animalLabel.font = .boldSystemFont(ofSize: 180)
        self.scaleAnim.fromValue = NSNumber(value: 1.5)
        self.scaleAnim.toValue = NSNumber(value: 0.25)
        self.scaleAnim.duration = 1.0
        
        self.addSubview(self.animalLabel)
        self.animalLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
    
    private func startAni() {
        self.animalLabel.tag = 0
        self.animalLabel.text = self.mtitleArr[self.animalLabel.tag]
        self.animalLabel.layer.add(self.scaleAnim, forKey: "scaleAnim")
    }
    
    
    ///
    /// - Parameters:
    ///   - durationTime: 每个数字动画的时长
    ///   - fromValue: 每个数字开始动画时数字的大小是原始大小的倍数
    ///   - toValue: 每个数字结束动画时数字的大小是原始大小的倍数
    ///   - fontSize: 每个数字的文字大小
    ///   - titleArr: 动画显示的文字
    public func setupConfig(_ durationTime: CGFloat = 1, fromValue: Float = 1.5, toValue: Float = 0.25, fontSize: CGFloat = 18, titleArr: [String] = ["3", "2", "1", "GO"]) {
        self.scaleAnim.duration = durationTime
        self.scaleAnim.fromValue = NSNumber(value: fromValue)
        self.scaleAnim.toValue = NSNumber(value: toValue)
        self.animalLabel.font = .boldSystemFont(ofSize: fontSize)
        self.mtitleArr = titleArr
    }
    
    public func start() {
        self.startAni()
    }
    
    public func returnDoneClosure(_ closure: @escaping () -> ()) {
        self.done = closure
    }
    
    //MARK: - lazy load
    private lazy var scaleAnim: CABasicAnimation = {
        let scaleAnim = CABasicAnimation()
        scaleAnim.keyPath = "transform.scale"
        scaleAnim.isRemovedOnCompletion = false
        scaleAnim.fillMode = .forwards
        scaleAnim.delegate = self
        scaleAnim.timingFunction = CAMediaTimingFunction(name: .easeOut)
        scaleAnim.repeatCount = 1
        return scaleAnim
    }()
    
    public lazy var animalLabel: Label = {
        let lbl = Label()
        lbl.frame = self.bounds
        lbl.tag = 0
        lbl.backgroundColor = .white
        lbl.text = self.mtitleArr[lbl.tag]
        lbl.textColor = .black
        lbl.textAlignment = .center
        return lbl
    }()
    
}

extension CountdownAnimationView: CAAnimationDelegate {
    public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        if anim == self.animalLabel.layer.animation(forKey: "scaleAnim") {
            if self.animalLabel.tag == self.mtitleArr.count - 1 {
                self.animalLabel.text = ""
                if self.done != nil {
                    self.done?()
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.removeFromSuperview()
                }
            } else {
                self.animalLabel.tag += 1
                self.animalLabel.text = self.mtitleArr[self.animalLabel.tag]
                self.animalLabel.layer.add(self.scaleAnim, forKey: "scaleAnim")
            }
        }
    }
}
